// pages/order/index.js
import { request } from "../../request/index.js";
import regeneratorRuntime from '../../lib/runtime/runtime';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[
      {
        id:0,
        value:"全部订单",
        isActive:true
      },
      {
        id:1,
        value:"待付款",
        isActive:false
      },
      {
        id:2,
        value:"待发货",
        isActive:false
      },
      {
        id:3,
        value:"退款/退货",
        isActive:false
      }
    ],
   //订单假数据  
      orders:[
        {
          number:'GD20190427000000000567',
          price:'279',
          time:'2019/4/27下午4：48：06'
        },
        {
          number:'GD20190427000000000568',
          price:'2343',
          time:'2019/4/27下午4：48：06'
        },
        {
          number:'GD20190427000000000569',
          price:'4234',
          time:'2019/4/27下午4：48：06'
        },
        {
          number:'GD20190427000000000570',
          price:'321',
          time:'2019/4/27下午4：48：06'
        },
        {
          number:'GD20190427000000000571',
          price:'2345',
          time:'2019/4/27下午4：48：06'
        }
      ]
  },
  onShow(options){
    const token= wx.getStorageSync("token");
    if(!token){
      wx.navigateTo({
        url: '/pages/auth/index',
      });
        
    }
      

    let pages =  getCurrentPages();
    let currentPage=pages[pages.length-1];
    console.log(currentPage.options);
      // 获取url上的type参数
      const {type} =currentPage.options;
    this.changeTitleIndex(type-1);
      this.getOrders(type);
  },
  async getOrders(type){
    const res=await request({url:"/my/orders/all",data:{type}});
    // console.log(res);
    this.setData({
      orders:res.orders
    })
  },
  // 根据索引来激活选中标题数组
  changeTitleIndex(index){
    // 2修改原数组
    let {tabs}=this.data;
    tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
    // 赋值到data中
    this.setData({
      tabs,orders
    })
  },
  // 标题点击事件
  tabsItemChange(e){
    // 1获取被点击的索引
    const {index}= e.detail;
    this.changeTitleIndex(index);
    // 点击重新发起请求
    this.getOrders(index+1);
  }   

})